package com.itheima52.mobilesafe.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.bean.BlackNumberInfo;
import com.itheima52.mobilesafe.db.dao.BlackNumberDao;
import com.itheima52.mobilesafe.utils.ToastUtils;

public class CallSafeActivity2 extends Activity {
	private ListView list_view;
	private List<BlackNumberInfo> blackNumberInfos;
	private LinearLayout ll_pb;
	private BlackNumberDao dao;
	private CallSafeAdapter adapter;

	private int startIndex = 0;// 开始的位置
	private int maxCount = 18;// 每一页加载的数据
	private int totalNumber;// 总的条目数
	private int lastVisiblePosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_call_safe2);
		initUI();
		initData();
	}

	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				// 加载完数据后,将玩命加载隐藏
				ll_pb.setVisibility(View.INVISIBLE);
				if (adapter == null) {
					adapter = new CallSafeAdapter();
					list_view.setAdapter(adapter);
				} else {
					adapter.notifyDataSetChanged();
				}

				break;

			default:
				break;
			}

		};

	};

	private void initData() {
		dao = new BlackNumberDao(CallSafeActivity2.this);
		totalNumber = dao.getTotalNumber();
		new Thread() {

			public void run() {

				// blackNumberInfos = dao.findAll();
				if (blackNumberInfos == null) {
					blackNumberInfos = dao.findPar2(startIndex, maxCount);
				} else {
					// 把后面的数据追加到当前的数据中,防止被覆盖
					blackNumberInfos.addAll(lastVisiblePosition + 1,
							dao.findPar2(startIndex, maxCount));
				}

				handler.sendEmptyMessage(0);

			};

		}.start();

	}

	private void initUI() {
		ll_pb = (LinearLayout) findViewById(R.id.ll_pb);

		// 展示加载的圆圈
		ll_pb.setVisibility(View.VISIBLE);

		list_view = (ListView) findViewById(R.id.list_view);
		// 设置list_view的滚动监听器
		list_view.setOnScrollListener(new OnScrollListener() {

			// 状态改变时候回调的方法
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				System.out.println("状态已经改变");
				switch (scrollState) {
				// 闲置状态
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:

					lastVisiblePosition = list_view.getLastVisiblePosition();
					System.out.println("lastVisiblePosition:"
							+ lastVisiblePosition);

					if (lastVisiblePosition == blackNumberInfos.size() - 1) {
						startIndex += maxCount;
						if (startIndex >= totalNumber) {
							ToastUtils.showToast(CallSafeActivity2.this,
									"已经没有数据了");
						}

						return;
					}
					initData();
					break;
				// 惯性滚动
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:

					break;
				// 手指触摸
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:

					break;

				default:
					break;
				}
			}

			// listview滚动的时候调用
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				System.out.println("正在滚动");
			}
		});

	}

	private class CallSafeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return blackNumberInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return blackNumberInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(CallSafeActivity2.this,
						R.layout.item_call_safe, null);
				holder = new ViewHolder();

				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_number);
				holder.tv_mode = (TextView) convertView
						.findViewById(R.id.tv_mode);
				holder.iv_delete = (ImageView) convertView
						.findViewById(R.id.iv_delete);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_number
					.setText(blackNumberInfos.get(position).getNumber());
			String mode = blackNumberInfos.get(position).getMode();
			if (mode.equals("1")) {
				holder.tv_mode.setText("来电+短信拦截");
			} else if (mode.equals("2")) {
				holder.tv_mode.setText("电话拦截");
			} else if (mode.equals("3")) {
				holder.tv_mode.setText("短信拦截");
			}
			final BlackNumberInfo blackNumberInfo = blackNumberInfos
					.get(position);
			holder.iv_delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String number = blackNumberInfo.getNumber();
					boolean result = dao.delete(number);
					if (result) {
						ToastUtils.showToast(CallSafeActivity2.this, "删除成功");
						blackNumberInfos.remove(blackNumberInfo);
						adapter.notifyDataSetChanged();
					} else {
						ToastUtils.showToast(CallSafeActivity2.this, "删除失败");
					}
				}
			});

			return convertView;
		}

		class ViewHolder {
			TextView tv_number;
			TextView tv_mode;
			ImageView iv_delete;
		}

	}

	/**
	 * 添加黑名单
	 * 
	 * @param view
	 */
	public void addBlackNumber(View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				CallSafeActivity2.this);
		final AlertDialog dialog = builder.create();
		View view2 = View.inflate(CallSafeActivity2.this,
				R.layout.dialog_add_black_number, null);
		final EditText et_number = (EditText) view2
				.findViewById(R.id.et_number);
		Button btn_ok = (Button) view2.findViewById(R.id.btn_ok);
		Button btn_cancel = (Button) view2.findViewById(R.id.btn_cancel);
		final CheckBox cb_phone = (CheckBox) view2.findViewById(R.id.cb_phone);
		final CheckBox cb_sms = (CheckBox) view2.findViewById(R.id.cb_sms);

		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();

			}
		});
		btn_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str_number = et_number.getText().toString().trim();
				if (TextUtils.isEmpty(str_number)) {
					ToastUtils.showToast(CallSafeActivity2.this, "请输入黑名单");
					return;
				}
				String mode = "";
				if (cb_phone.isChecked() && cb_sms.isChecked()) {
					mode = "1";
				} else if (cb_phone.isChecked() && !cb_sms.isChecked()) {
					mode = "2";
				} else if (!cb_phone.isChecked() && cb_sms.isChecked()) {
					mode = "3";
				} else {
					ToastUtils.showToast(CallSafeActivity2.this, "请勾选拦截模式");
					return;
				}
				BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
				blackNumberInfo.setNumber(str_number);
				blackNumberInfo.setMode(mode);
				blackNumberInfos.add(0, blackNumberInfo);

				dao.add(str_number, mode);
				if (adapter == null) {
					adapter = new CallSafeAdapter();
					list_view.setAdapter(adapter);
				} else {
					adapter.notifyDataSetChanged();
				}
				dialog.dismiss();

			}
		});
		dialog.setView(view2);
		dialog.show();

	}
}
